package com.skytech.tax.customsdatawarehouse.electronicport.customsdeclarations.domain.dao;

import com.skytech.tax.customsdatawarehouse.electronicport.customsdeclarations.domain.entity.CustomsDeclaration;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/6/10
 */
@Repository("electronicPort.customsDeclarationDao")
public class CustomsDeclarationDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectCustomsDeclarationsSql = "SELECT entryId, customsDeclaration, storageTime, iEDate, socialCreditCode FROM gd_cardhead WHERE 1=1";

    public List<CustomsDeclaration> findAll() {
        return namedParameterJdbcTemplate.getJdbcOperations()
                .query(this.selectCustomsDeclarationsSql, new BeanPropertyRowMapper<>(CustomsDeclaration.class));
    }

    public List<CustomsDeclaration> findByEntryIds(List<String> entryIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("entryIds", entryIds);
        return namedParameterJdbcTemplate.query(this.selectCustomsDeclarationsSql + " AND entryId in (:entryIds)", params, new BeanPropertyRowMapper<>(CustomsDeclaration.class));
    }

    private String saveCustomsDeclarationsSql = "INSERT INTO gd_cardhead" +
            "(entryId, customsDeclaration, storageTime, iEDate, socialCreditCode, decType, flagDec, opMode) " +
            "VALUES (:entryId, :customsDeclaration, :storageTime, :iEDate, :socialCreditCode, :decType, :flagDec, :opMode)";

    public void save(CustomsDeclaration customsDeclaration) {
        customsDeclaration.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.saveCustomsDeclarationsSql,
                new BeanPropertySqlParameterSource(customsDeclaration));
    }

    private String deleteCustomsDeclarationSql = "DELETE FROM gd_cardhead WHERE entryId = ?";

    public void delete(String entryId) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteCustomsDeclarationSql, entryId);
    }

    public CustomsDeclaration findByEntryId(String entryId) {
        StringBuilder sql = new StringBuilder(this.selectCustomsDeclarationsSql);
        List<Object> params = new ArrayList<>();
        if (StringUtils.isNotEmpty(entryId)) {
            sql.append(" AND entryId = ? ");
            params.add(entryId);
        }
        try {
            return namedParameterJdbcTemplate.getJdbcOperations()
                    .queryForObject(sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(CustomsDeclaration.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    private String updateCustomsDeclarations = "update gd_cardhead set decType=:decType, flagDec=:flagDec, opMode=:opMode, modifyTime=:modifyTime where entryId=:entryId";

    public void update(String entryId, String decType, String flagDec, String opMode) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("entryId", entryId);
        params.addValue("decType", decType);
        params.addValue("flagDec", flagDec);
        params.addValue("opMode", opMode);
        params.addValue("modifyTime", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.updateCustomsDeclarations, params);
    }
}
